<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>详情</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script src="./js/jquery-1.11.1.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/main.js"></script>
    <style type="text/css">
        body {
            background: #2fb9e2;
        }
        
        .content {
            margin: auto;
            margin-top: 100px;
            padding: 7%;
            padding-bottom: 20px;
            width: 800px;
            height: auto;
            background-color: #fff;
            border-radius: 20px;
            box-shadow: 0px 0px 20px #5b7884;
        }
        
        .table {
            text-align: center;
            margin: auto;
        }
        
        #add_dish {
            float: right;
        }
        
        #title {
            margin: auto;
            padding-top: 5px;
            padding-bottom: 5px;
        }
        
        .row {
            padding-bottom: 20px;
            font-size: 16px;
        }
        
        .table img {
            width: 100px;
        }
        
        .table td {
            text-align: center;
        }

        .center{
            text-align: center;
            margin-bottom: 50px;
        }
        
        img {
            margin-left:0px;
            text-align:left;
            padding-left:0px;
            width:150px;
        }
    </style>
</head>
<script>
    var pid;
    var session = getCookie('sessionid');
    //if (!session){
        //alert("没有登录")
        //    window.location.href = "login.html"
        //}
    $(document).ready(function() {
        pid = QueryString.id;
        //alert(pid);
        
        if(!pid){
            //alert("没有id");
            //window.location.href = "manage.html"
            return
        }
        getProviderDetail(pid);
        getDishes(pid);
        
        $("#confirm_delete").click(function(){
            var did = $("#delete_id").val();
            //alert(id);
            deleteDish(did);
        })
        
        $("#confirm_add").click(function(){
            var data = getInputDataAdd();
            if(!checkData(data)){
                return;
            }

            addDish(pid,data.name,data.left,data.price,data.original_price);
        });
        $("#confirm_modify").click(function(){
            var data = getInputDataModify();
            if(!checkData(data)){
                return;
            }
            modifyDish(data.did,data.name,data.left,data.price,data.original_price);
        });
    })
    function checkData(data){
        var flag = true;
        $.each(data,function(name,value){
            if (name=="left" || name == "price" || name == "original_price"){
                if(!checkInputNumber(name,value)){
                    flag = false;
                }
            }else{
                if(!checkInput(name,value)){
                    flag = false;
                }
            }
        })
        return flag;
    }
    
    function getInputDataAdd(){
        var data = new Object();
        data.name = $("#dish_name_add").val();
        data.left = $("#left_add").val();
        data.price = $("#price_add").val();
        data.original_price = $("#original_price_add").val();
        return data;
    }
    function getInputDataModify(){
        var data = new Object();
        data.name = $("#dish_name_modify").val();
        data.left = $("#left_modify").val();
        data.price = $("#price_modify").val();
        data.original_price = $("#original_price_modify").val();
        data.did = $("#did_modfiy").val();
        return data;
    }
    function getProviderDetail(id){
        //var session = getCookie('sessionid');
        //alert("dish:" + session + "," + id);
        //访问网络
        var settings = {
                type: "POST",
                url: "../../Fanxiangle/servlet/manager",
                dataType: "json",
                data: {
                    module: "provider",
                    action: "getProviderDetails",
                    pid: id,
                    sessionid: session
                },
                error: function(XHR, textStatus, errorThrown) {
                    alert("网络错误");
                },
                success: function(data, textStatus) {
                    if (textStatus == "success") {
                        if (data.status == true) {
                            $("#dama_name").text(data.data.name);
                            $("#rating").text(data.data.rating.rating + "分/" + data.data.rating.num + "人");
                            $("#address").text(data.data.address);
                            $("#delivermin").text(data.data.delivermin + "元起送");
                            $("#delivercost").text(data.data.delivercost + "元");
                            $("#status").text(getStatusString(data.data.status));
                            $("#notice").text(data.data.notice);
                            $("#description").text(data.data.description);
                        } else if (data.errorCode == 1001) {
                            alert("参数不全")
                        } else if (data.errorCode == 1002) {
                            alert("尚未登录")
                            window.location.href = "login.html"
                        } else if (data.errorCode == 1006) {
                            alert("无此接口")
                        } else if (data.errorCode == 1010) {
                            alert("数据库错误")
                        } else if (data.errorCode == 1000) {
                            alert("未知错误")   
                        }
                        //alert("data:" + data.data + ",status:" + textStatus)
                    }else{
                        alert("服务器错误");
                    }

                }
            };
            $.ajax(settings);
    }
    //pid
    function getDishes(id){
        //var session = getCookie('sessionid');
        //alert(session + "," + id);
        //访问网络
        var settings = {
                type: "POST",
                url: "../../Fanxiangle/servlet/manager",
                dataType: "json",
                data: {
                    module: "dish",
                    action: "getDishList",
                    pid: id,
                    sessionid: session
                },
                error: function(XHR, textStatus, errorThrown) {
                    alert("网络错误");
                },
                success: function(data, textStatus) {
                    if (textStatus == "success") {
                        if (data.status == true) {
                            //alert("data:" + data.data);
                            var colTr = '<tr><td>图片</td><td>菜品名</td><td>待售数量</td><td>菜品价格</td><td colspan=2>操作</td></tr>'
                            var contentTr;
                            $.each(data.data,function(index,value){
                                contentTr += addDishRow(value.did,value.name, value.left, value.price, value.original_price);
                            })
                            
                            var tableTr = colTr + contentTr;
                            $("#content_table").html(tableTr);
                            
                        } else if (data.errorCode == 1001) {
                            alert("参数不全")
                        } else if (data.errorCode == 1002) {
                            alert("尚未登录")
                            window.location.href = "login.html"
                        } else if (data.errorCode == 1006) {
                            alert("无此接口")
                        } else if (data.errorCode == 1010) {
                            alert("数据库错误")
                        } else if (data.errorCode == 1000) {
                            alert("未知错误")   
                        }
                        //alert("data:" + data.data + ",status:" + textStatus)
                    }else{
                        alert("服务器错误");
                    }

                }
            };
            $.ajax(settings);
    }
    
    function getStatusString(status){
         var str = new Array("休息中","接受预订","开点中");
        return str[status];
    }
    function delete_dish(did,name){
        $("#delete_id").val(did);
        $("#delete_name").text(name);
        
        $("#modal_delete_dish").modal('show');
    }
    function modify_dish(did,name,left,price,original_price){
        $("#dish_name_modify").val(name);
        $("#left_modify").val(left);
        $("#price_modify").val(price);
        $("#original_price_modify").val(original_price);
        $("#did_modfiy").val(did);
        $("#img_modify").attr('src','./images/dishes/covers/'+did+'.png');
        $("#modal_modify_dish").modal('show');
    }
    function deleteDish(id){
        var settings = {
                type: "POST",
                url: "../../Fanxiangle/servlet/manager",
                dataType: "json",
                data: {
                    module: "dish",
                    action: "deleteDish",
                    did: id,
                    sessionid: session
                },
                error: function(XHR, textStatus, errorThrown) {
                    alert("网络错误");
                },
                success: function(data, textStatus) {
                    if (textStatus == "success") {
                        if (data.status == true) {
                            alert("删除成功");
                            $("#modal_delete_dish").modal('hide');
                            getDishes(pid)
                            
                        } else if (data.errorCode == 1001) {
                            alert("参数不全")
                        } else if (data.errorCode == 1002) {
                            alert("尚未登录")
                            window.location.href = "login.html"
                        } else if (data.errorCode == 1006) {
                            alert("无此接口")
                        } else if (data.errorCode == 1010) {
                            alert("数据库错误")
                        } else if (data.errorCode == 1000) {
                            alert("未知错误")   
                        }
                        //alert("data:" + data.data + ",status:" + textStatus)
                    }else{
                        alert("服务器错误");
                    }

                }
            };
            $.ajax(settings);
    }
    //did
    function modifyDish(id,name,left,price,oprice){
        var dataSend = new Object();
        dataSend.module = "dish";
        dataSend.action = "modifyDish";
        dataSend.sessionid = session;
        
        dataSend.name = name;
        dataSend.left = left;
        dataSend.price = price;
        dataSend.original_price = oprice;
        dataSend.did = id;
        
        var data = new FormData();
        $.each(dataSend,function(name,value){
            data.append(name,value);
        });
        var simpleFile = document.getElementById("file_modify").files[0];
        
        if (simpleFile == undefined) {
            //alert("没有修改菜品照片");
            //return;
        }else {
            data.append('cover', simpleFile);
        }
        
        //访问网络
        var settings = {
                type: "POST",
                url: "../../Fanxiangle/servlet/manager",
                //dataType: "json",
                processData: false,
                contentType: false,
                cache: false,
                data: data,
                error: function(XHR, textStatus, errorThrown) {
                    alert("网络错误");
                },
                success: function(row, textStatus) {
                    var data = JSON.parse(row);
                    //alert(data.status + "," + textStatus);
                    if (textStatus == "success") {
                        if (data.status == true) {
                            getDishes(pid)
                            alert("修改成功");
                            $("#modal_modify_dish").modal('hide');
                            //window.location.href = "manage.html"
                        } else if (data.errorCode == 1001) {
                            alert("参数不全")
                        } else if (data.errorCode == 1002) {
                            alert("尚未登录")
                            window.location.href = "login.html"
                        } else if (data.errorCode == 1006) {
                            alert("无此接口")
                        } else if (data.errorCode == 1010) {
                            alert("数据库错误")
                        } else if (data.errorCode == 1000) {
                            alert("未知错误")
                            $("#modal_confirm").modal('hide');
                        }
                        //alert("data:" + data.data + ",status:" + textStatus)
                    }else{
                        alert("服务器错误");
                    }

                }
            };
            $.ajax(settings);
    }
    //pid
    function addDish(id,name,left,price,oprice){
        var session = getCookie('sessionid');
        //alert(session + "," + id);
        var dataSend = new Object();
        dataSend.module = "dish";
        dataSend.action = "addDish";
        dataSend.sessionid = session;
        
        dataSend.name = name;
        dataSend.left = left;
        dataSend.price = price;
        dataSend.original_price = oprice;
        dataSend.pid = id;
        
        var data = new FormData();
        $.each(dataSend,function(name,value){
            data.append(name,value);
        });
        var simpleFile = document.getElementById("file_add").files[0];
        if (simpleFile == undefined) {
            alert("没有添加菜品照片");
            return;
        }else {
            data.append('cover', simpleFile);
        }
               
          
        //访问网络
        var settings = {
                type: "POST",
                url: "../../Fanxiangle/servlet/manager",
                //dataType: "json",
                processData: false,
                contentType: false,
                cache: false,
                data: data,
                error: function(XHR, textStatus, errorThrown) {
                    alert("网络错误");
                },
                success: function(row, textStatus) {
                    var data = JSON.parse(row);
                    //alert(data.status + "," + textStatus);
                    if (textStatus == "success") {
                        if (data.status == true) {
                            getDishes(pid);
                            alert("修改成功");
                            
                            $("#modal_add_dish").modal('hide');
                            //window.location.href = "manage.html"
                        } else if (data.errorCode == 1001) {
                            alert("参数不全")
                        } else if (data.errorCode == 1002) {
                            alert("尚未登录")
                            window.location.href = "login.html"
                        } else if (data.errorCode == 1006) {
                            alert("无此接口")
                        } else if (data.errorCode == 1010) {
                            alert("数据库错误")
                        } else if (data.errorCode == 1000) {
                            alert("未知错误")
                            $("#modal_confirm").modal('hide');
                        }
                        //alert("data:" + data.data + ",status:" + textStatus)
                    }else{
                        alert("服务器错误");
                    }

                }
            };
            $.ajax(settings);
    }
    
    function addDishRow(id,name,left,price,original_price){
        var contentTr = '<tr id="' + id + '">';
        contentTr += '<td><img src="http://182.92.68.128/images/dishes/covers/'+ id + '.png" alt="尚未添加图片"/></td>';
        contentTr += '<td>' + name + '</td>';
        contentTr += '<td>' + left + '</td>';
        contentTr += '<td>' + price + '/' + original_price + '</td>';
        contentTr += '<td>';
        contentTr += '<button class="btn btn-sm btn-default" onclick="modify_dish('+ id +',\''+name+'\',' + left + ',' + price + ',' + original_price + ')">修改</button>';
        contentTr += '<button class="btn btn-sm btn-danger" onclick="delete_dish(' + id +',\''+name+'\')">删除</button>';
        contentTr += '</td>';
        contentTr + '</tr>';
        return contentTr;
    }

    function back () {
        window.location.href = "manage.html"
    }
    // 将文件以Data URL形式进行读入页面
    function readAsDataURL(i){
        //alert(i);
        // 检查是否为图像类型
        var simpleFile;
        if (i == 1){
            simpleFile = document.getElementById("file_add").files[0];
        }else if (i == 2) {
            simpleFile = document.getElementById("file_modify").files[0];
        }
        if (simpleFile == undefined) {
            alert("您还没有选择图片")
            return;
        };
        if(!/image\/\w+/.test(simpleFile.type)) {
            alert("请确保文件类型为图像类型");
            return false;
        }
        var reader = new FileReader();
        // 将文件以Data URL形式进行读入页面
        reader.readAsDataURL(simpleFile);
        reader.onload = function(e){
            var m;
            if(i==1){
                m = $("#img_add");
            }else if (i == 2 ) {
                m = $("#img_modify");
            }
            m.attr("src",this.result)
            console.log(this.result);
        }
    }
    
</script>
<body>
<div class="modal fade" id="modal_add_dish">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <!-- <span aria-hidden="true">&times;</span>
                    -->
                </button>
                <h4 class="modal-title">添加菜品信息</h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="name">菜品名</label>
                        <input type="text" class="form-control" id="dish_name_add" name="name" placeholder="必选"></div>
                    
                    <div class="form-group">
                        <label for="left">待售数量</label>
                        <input type="text" class="form-control" id="left_add" name="left" placeholder="必选"></div>
                    <div class="form-group">
                        <label for="price">菜品价格</label>
                        <input type="text" class="form-control" id="price_add" name="price" placeholder="必选"></div>
                    <div class="form-group">
                        <label for="original_price">菜品原价</label>
                        <input type="text" class="form-control" id="original_price_add" name="original_price" placeholder="必选"></div>
                </form>     
                    <div class="form-group">
                        <label for="file">上传菜品照片</label>
                        <input type="file" class="col-sm-12" id="file_add" name="cover" onchange="readAsDataURL(1)">
                    </div>
                    <img src="./logo.png" alt="" id="img_add" class="image-rounded">
                
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="confirm_add">提交</button>
            </div>
        </div>
    </div>
</div>
    <div class="modal fade" id="modal_modify_dish">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <!-- <span aria-hidden="true">&times;</span>
                    -->
                </button>
                <h4 class="modal-title">添加菜品信息</h4>
            </div>
            <div class="modal-body">
                <form>
                    <input type="hidden" name="module" value="admin">
                    <input type="hidden" name="action" value="login">
                    <div class="form-group">
                        <label for="name">菜品名</label>
                        <input type="text" class="form-control" id="dish_name_modify" name="name" placeholder="必选"></div>
                    
                    <div class="form-group">
                        <label for="left">待售数量</label>
                        <input type="text" class="form-control" id="left_modify" name="left" placeholder="必选"></div>
                    <div class="form-group">
                        <label for="price">菜品价格</label>
                        <input type="text" class="form-control" id="price_modify" name="price" placeholder="必选"></div>
                    <div class="form-group">
                        <label for="original_price">菜品原价</label>
                        <input type="text" class="form-control" id="original_price_modify" name="original_price" placeholder="必选"></div>
                    <input type="hidden" id="did_modfiy"></form>
                        
                    <div class="form-group">
                        <label for="file">上传菜品照片</label>
                        <input type="file" class="col-sm-12" id="file_modify" name="cover" onchange="readAsDataURL(2)">
                        
                        
                    </div>
                    <img src="./logo.png" alt="" id="img_modify" class="image-rounded">  
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="confirm_modify">提交</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="modal_delete_dish">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <!-- <span aria-hidden="true">&times;</span>
                -->
            </button>
            <h4 class="modal-title">饭享乐-删除菜品</h4>
        </div>
        <div class="modal-body">
            <p>
                是否需要删除菜品：
                <span id="delete_name"></span>
            </p>
            <input type="hidden" value="" id="delete_id"></div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-danger" id="confirm_delete">删除</button>
        </div>
    </div>
    <!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<div class="container">
    <div class="content">
        <h1 class="center">饭享乐－大妈详情</h1>
        <div class="row">
            <div class="col-xs-3">姓名</div>
            <div class="col-xs-5" id="dama_name"></div>
        </div>
        <div class="row">
            <div class="col-xs-3">餐厅评分</div>
            <div class="col-xs-5" id="rating"></div>
        </div>
        <div class="row">
            <div class="col-xs-3">地址</div>
            <div class="col-xs-5" id="address"></div>
        </div>
        <div class="row">
            <div class="col-xs-3">起送价格</div>
            <div class="col-xs-5" id="delivermin"></div>
        </div>
        <div class="row">
            <div class="col-xs-3">配送费用</div>
            <div class="col-xs-5" id="delivercost"></div>
        </div>
        <div class="row">
            <div class="col-xs-3">当前状态</div>
            <div class="col-xs-5" id="status"></div>
        </div>
        <div class="row">
            <div class="col-xs-3">餐厅公告</div>
            <div class="col-xs-5" id="notice"></div>
        </div>
        <div class="row">
            <div class="col-xs-3">餐厅简介</div>
            <div class="col-xs-5" id="description"></div>
        </div>
        <div class="panel panel-info">

            <div class="panel-heading">
                <button class="btn btn-sm btn-primary" id="add_dish" data-toggle="modal" data-target="#modal_add_dish">添加</button>
                <p id="title"> <strong>菜品列表</strong>
                </p>
            </div>

            <div class="list-group">

                <table class="table table-striped" id="content_table">
                </table>
            </div>

        </div>
        <div class="row">
            <button class="btn btn-default center-block" onclick="back()">返回</button>
        </div>
    </div>
</div>
</body>

</html>